﻿#include "actionform.h"
#include "ui_actionform.h"
#include <AppKey/appkey.h>
#include <QClipboard>
#include <QGraphicsDropShadowEffect>
#include <QMouseEvent>
#include <QMovie>
#include <QPainter>
#include <QMessageBox>

ActionForm::ActionForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ActionForm)
{
    ui->setupUi(this);

    timer = new  QTimer(this);


    connect(timer, &QTimer::timeout, this, &ActionForm::update);
    ui->lineEdit_Local->setText(AppKey::Instance()->get_SerialNumber());


    initUI();
    initForm();
    checkApp();
}

ActionForm::~ActionForm()
{
    delete ui;
}

void ActionForm::initForm()
{
    QMovie *movie = new QMovie(":/img/main.gif");
    QSize size(ui->log->width(),ui->log->height());
    movie->setScaledSize(size);

    movie->start();         // 2. 启动动画
    ui->log->setMovie(movie); // 1. 设置要显示的 GIF 动画图片
    ui->log->show();

    ui->Local_GB->hide();

    ui->progressBar->setValue(0);

}

void ActionForm::initUI()
{
    setWindowFlags(Qt::FramelessWindowHint);
}

void ActionForm::setCompanyName(QString info)
{
    ui->companyName->setText(info);
}

void ActionForm::setSoftName(QString info)
{
    ui->softName->setText(info);
}

void ActionForm::setSoftVersion(QString info)
{
    ui->version->setText(info);
}

void ActionForm::setCompanyInfo(QString info)
{
    ui->companyInfo->setText(info);
}


void ActionForm::checkApp()
{
    m_nState = AppKey::Instance()->check();
    strList << tr("Perform software key detection") //进行软件密钥检测
            << tr("Check whether the file exists")  //检测文件是否存在
            << tr("Read key file")                  //读取密钥文件
            << tr("Perform ciphertext decryption")  //进行密文解密
            << tr("Detects key file contents")      //检测密钥文件内容
            << tr("Check whether the mechanical code is consistent") //检测机械码是否一致
            << tr("Service life of testing equipment"); //检测设备使用期限

    ui->progressBar->setMaximum(strList.size()*100);
    timer->start(50);
}

void ActionForm::update()
{
    QString info =  QString("%1[P:%2%]").arg(strList.at(m_nIndex)).arg(m_nCout);

    if(m_nState == -1 && m_nIndex == 1){
        timer->stop();
        info =  QString("%1[P:%2%][Error:%3][Code:%4]").arg(strList.at(m_nIndex)).arg(m_nCout).arg(tr("The key file does not exist")).arg(m_nState);
        ui->message->setStyleSheet("color:red;");
        ui->Local_GB->show();
    }

    if(m_nState == -2 && m_nIndex == 3){
        timer->stop();
        info =  QString("%1[P:%2%][Error:%3][Code:%4]").arg(strList.at(m_nIndex)).arg(m_nCout).arg(tr("The key file content is incorrect")).arg(m_nState);
        ui->message->setStyleSheet("color:red;");
        ui->Local_GB->show();
    }

    if(m_nState == -3 && m_nIndex == 5){
        timer->stop();
        info =  QString("%1[P:%2%][Error:%3][Code:%4]").arg(strList.at(m_nIndex)).arg(m_nCout).arg(tr("Mechanical codes are inconsistent")).arg(m_nState);
        ui->message->setStyleSheet("color:red;");
        ui->Local_GB->show();
    }


    if(m_nState == -4 && m_nIndex == 6){
        timer->stop();
        info =  QString("%1[P:%2%][Error:%3][Code:%4]").arg(strList.at(m_nIndex)).arg(m_nCout).arg(tr("Expired equipment")).arg(m_nState);
        ui->message->setStyleSheet("color:red;");
        ui->Local_GB->show();
    }

    if (m_nState == -5 && m_nIndex == 6) {
		timer->stop();
		info = QString("%1[P:%2%][Error:%3][Code:%4]").arg(strList.at(m_nIndex)).arg(m_nCout).arg(tr("Expired equipment")).arg(m_nState);
		ui->message->setStyleSheet("color:red;");
		ui->Local_GB->show();
	}



    ui->message->setText(info);
    m_nCout += 40 - m_nIndex;
    if(m_nCout > 100){
        m_nCout = 0;
        m_nPro += 100;
        m_nIndex++;
        if(m_nIndex==7){
            m_nIndex = 0;
            timer->stop();
            emit done();
            this->setVisible(false);
        }
    }
    ui->progressBar->setValue(m_nPro + m_nCout);
}

void ActionForm::mouseMoveEvent(QMouseEvent *event)
{
    QPoint y = event->globalPos();//鼠标相当于桌面左上角的位置，鼠标全局位置
    QPoint x = y - this->z;
    this->move(x);
}

void ActionForm::mousePressEvent(QMouseEvent *event)
{
    QPoint y = event->globalPos();//鼠标相当于桌面左上角的位置，鼠标全局位置
    QPoint x = this->geometry().topLeft();//窗口左上角位于桌面左上角的位置，窗口位置
    this->z = y - x; //定值，不变
}

void ActionForm::mouseReleaseEvent(QMouseEvent *event)
{
    this->z = QPoint(); //鼠标松开获取当前的坐标
}

void ActionForm::on_Bt_copy_clicked()
{
    QString strLocal = ui->lineEdit_Local->text();
    if(!strLocal.isEmpty()){
        QClipboard *clip = QApplication::clipboard();
        clip->setText(strLocal);
        QMessageBox::information(this, tr("info"), tr("Copy successfully!"));
    }
}

void ActionForm::on_Bt_close_clicked()
{
    if(!QMessageBox::information(this, tr("info"), tr("Exit the App!"), tr("Yes"), tr("No"))){
        emit appQuit();
        this->close();
    }
}



void ActionForm::on_Bt_create_clicked()
{
    QString strKey = ui->lineEdit_key->text();
    if(strKey.isEmpty()){
        return;
    }


    QString src = strKey;
    QByteArray text = src.toLocal8Bit();
    QByteArray by = text.fromBase64(text);
    QString str = QString::fromLocal8Bit(by); // 注意：这里是处理中文用的


    AppKey::Instance()->createKey(str);
    if(!QMessageBox::information(this, tr("info"), tr("Create successfully!"), tr("Yes"), tr("No"))){
        this->close();
    }
}
